package com.cyd.product_service;

import com.cyd.product_service.cluster.ClusterService;
import com.cyd.product_service.cluster.consul.ConsulServiceRegister;
import com.cyd.product_service.service.thrift.StudentService;
import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
//@EnableDiscoveryClient
//@RestController
public class ProductServiceApplication  implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(ProductServiceApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }

    @Autowired
    private StudentService.Iface studentInf;

    private ClusterService service;

//    @Autowired
//    private  ConsulServiceRegister consulServiceRegister;

    /**
     * 向Spring注册一个Bean对象
     * initMethod = "start"  表示会执行名为start的方法
     * start方法执行之后，就会阻塞接受客户端的请求
     *
     * @return
     */
//    @Bean(initMethod = "start")
//    public ThriftServer2 init() {
//        ThriftServer2 thriftServer = new ThriftServer2();
//        return thriftServer;
//    }

    /**
     * 健康检查，给consul服务发现组件调用的
     *
     * @return
     */
    @GetMapping("/check")
    public String health() {
        return "I'm OK!--risk Server";
    }

    @Override
    public void run(String... args) throws Exception {

        TMultiplexedProcessor processor = new TMultiplexedProcessor();
        processor.registerProcessor("studentInf", new StudentService.Processor<>(studentInf));

        TNonblockingServerTransport transport =new TNonblockingServerSocket(8889, 5000);
        TThreadedSelectorServer.Args serverArgs =new TThreadedSelectorServer.Args(transport);
        serverArgs.processor(processor);
        serverArgs.transportFactory(new TFramedTransport.Factory(1 << 11));
        serverArgs.protocolFactory(new TBinaryProtocol.Factory());
        serverArgs.maxReadBufferBytes = 1 << 12;
        TThreadedSelectorServer server = new TThreadedSelectorServer(serverArgs);
        log.info("Start server, listening at port: 8888" );


        service = new ClusterService();
        service.setId("aaaaa");
        service.setName("student-server");
        service.setAddress("127.0.0.1");
        service.setPort(8500);
        service.setServiceType(ClusterService.ServiceType.TCP);
        service.setWeight(1);

        ConsulServiceRegister consulServiceRegister= new ConsulServiceRegister();
        consulServiceRegister.register(service);
        Runtime.getRuntime().addShutdownHook(
                new Thread(() -> consulServiceRegister.deregister(service)));
        server.serve();

    }

}
